<template>
  <div>
    <h1>{{ msg }}  当前路径:{{this.$router.currentRoute.fullPath}}</h1>
    <router-link to="/">Home</router-link>

  </div>
</template>

<script>
import md5 from 'js-md5'
import axios from 'axios'
export default {
  name: 'ZhangYi',
  data () {
    return {
      msg: '欢迎来到菜鸟教程zhangyi',
      site: '菜鸟是你爸爸zhangyi',
      use: false,
      message: 'hhelo zhangyi',
      message2: 'zhangyi world',
      ak: '',
      sk: ''
    }
  },
  methods: {
    details: function (ak, sk) {
      var req = {
        method: 'post',
        url: 'http://127.0.0.1:9400/auth/v1/login',
        withCredentials: true,
        route: this.$router
      }
      axios.post(req.url, '{}').then(function (result) {
        console.log('登录成功' + result)
      }).catch(function (e) {
        console.log('登录异常' + ':' + req.route)
        var route = req.route
        if (e.response.status === 401) {
          var authHeader = e.response.headers.authenticate
          if (authHeader !== '') {
            var validAuth = authHeader.slice(6)
            var strings = validAuth.split(',')
            var json = {}
            strings.forEach(item => {
              var strings2 = item.split('=', 2)
              json[strings2[0].trim()] = strings2[1].replaceAll('\'', '').trim()
            })
            let realm = json['realm']
            var qop = json['qop']
            var nonce = json['nonce']
            var cnonce = '00000001'
            var nc = '00000001'
            var uri = '/VIID/auth/v1/login'
            var paramas1 = ak + ':' + realm + ':' + sk
            var params2 = 'POST:/VIID/auth/v1/login'
            var params3 = md5(paramas1) + ':' + nonce + ':' + nc + ':' + ':' + cnonce +
            ':' + qop + ':' + md5(params2)
            var responseStr = md5(params3)
            var authorization = 'Digest username=' + ak + ',realm=' + realm + ',nonce=' + nonce + ',uri=' + uri +
              ',response=' + responseStr + ',cnonce=' + cnonce + ',qop=' + qop + ',nc=' + nc
            axios.post('http://127.0.0.1:9400/auth/v1/login', {}, {headers: {
              'authorization': authorization}}).then(function () {
              console.log(route)
              alert('登录流程完成')
              route.push({ name: 'HelloWorld', query: {name: 'word', age: '1'} })
            }).catch(function (e) {
              console.log(e)
              console.log(route)
              route.push({ name: 'HelloWorld', query: {name: 'word', age: '1'} })
            })
          }
        }
      })
      console.log(this.$router)
      return md5(this.msg + '学习')
    },
    hello: function () {
      console.log('hello')
    }
  }
}
</script>

<!-- Add 'scoped' attribute to limit CSS to this component only -->
<style scoped>
  h1, h2 {
    font-weight: normal;
  }
  ul {
    list-style-type: none;
    padding: 0;
  }
  li {
    display: inline-block;
    margin: 0 10px;
  }
  a {
    color: #42b983;
  }

  .class1{
    background: #444;
    color: #eee;
  }
</style>
